package com.ipfsemc.ipfsemc.service.imp;

import com.ipfsemc.ipfsemc.mapper.BountyMapper;
import com.ipfsemc.ipfsemc.mapper.UserInfoMapper;
import com.ipfsemc.ipfsemc.model.Bounty;
import com.ipfsemc.ipfsemc.model.UserInfo;
import com.ipfsemc.ipfsemc.service.IBountyService;
import com.ipfsemc.ipfsemc.util.CodecUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class BountyServiceImpl implements IBountyService {
    @Autowired
    private BountyMapper bountyMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public int addBounty(Bounty bounty) {
        int n=-1;
        bounty.setBountyId(CodecUtil.createId());

        UserInfo userInfo = userInfoMapper.selectOne(bounty.getUserId());
        //奖励金大于提现余额
        if (null!=userInfo.getBounty()&&userInfo.getBounty().compareTo(bounty.getBountyMoney())!=-1){

            UserInfo u=new UserInfo();

            u.setFrozen(bounty.getBountyMoney());
            u.setUserId(bounty.getUserId());
            int i = userInfoMapper.updateByPrimaryKey(u);
            n=bountyMapper.insertSelective(bounty);
        }

        return n;
    }

    @Override
    public int delBounty(String bountyId) {

        UserInfo u=new UserInfo();
        Bounty b = bountyMapper.selectByPrimaryKey(bountyId);

        //如果不是已打款状态   修改用户冻结奖励金
        if(b.getBountyState()!=1 || b.getBountyState()!=-2){
            u.setFrozen(b.getBountyMoney().negate());
            u.setUserId(b.getUserId());
            int i = userInfoMapper.updateByPrimaryKey(u);
        }

        return bountyMapper.deleteByPrimaryKey(bountyId);
    }

    @Override
    public List<Bounty> listBounty(Bounty bounty) {
        return bountyMapper.listBounty(bounty);
    }

    @Override
    public List<Bounty> listBountyState(Bounty bounty) {
        return bountyMapper.listBountyState(bounty);
    }
}
